//
//  YCStack.swift
//  Stack
//
//  Created by YC on 2018/7/26.
//  Copyright © 2018年 YC. All rights reserved.
//

import UIKit

struct YCStack<Element> {
    fileprivate var array: [Element] = []
    
    var isEmpty: Bool { return array.isEmpty }
    var count: Int { return array.count }
    
    mutating func clear() {
        self.array.removeAll()
    }
    
    mutating func push(_ element: Element) {
        array.append(element)
    }
    
    @discardableResult mutating func pop() -> Element? {
        return array.popLast()
        
    }
    
    mutating func reverse() {
        self.array.reverse()
    }
    
    func peek() -> Element? {
        return array.last
    }
}

//MARK: - CustomStringConvertible
extension YCStack: CustomStringConvertible {
    var description: String {
        let topDivider = "---Stack---\n"
        let bottomDivider = "\n-----------\n"

        let stackElements = array.map { "\($0)" }.reversed().joined(separator: "\n")
        
        return topDivider + stackElements + bottomDivider
    }
}
